const cloud = require('wx-server-sdk');
cloud.init();
exports.main = async (event) => {
  const db = cloud.database()
  const _ = db.command
  let res = [];
  if (event.status === 'student') {
    res = (await db.collection('course').get()).data.filter((value) => value.students.indexOf(event.code) !== -1)
  } else if (event.status === 'teacher') {
    res = (await db.collection('course').where({ teacherId: event.code }).get()).data
  } else if (event.status === 'admin') {
    res = (await db.collection('course').where({ code: event.code }).get()).data
  }
  res.sort((a, b) => a.endDate < b.endDate ? 1 : -1)
  res = res.filter((value) => value.active)
  return res
}
